package com.bw.contorller;

import com.bw.entity.AddTable;
import com.bw.service.AddTableService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AddTableContorller {
    @Autowired
    private AddTableService addTableService;

    @RequestMapping("list")
    public String list(AddTable addTable, Model model, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        PageInfo<AddTable> info=addTableService.list(addTable,pageNum,pageSize);
        model.addAttribute("a",addTable);
        model.addAttribute("info",info);
        return "list";
    }

    @RequestMapping("selectAddtable")
    public String selectAddtable(AddTable addtable,Model model){
        System.out.println(addtable+"**********************************************************************");
       AddTable addTable= addTableService.selectAddtable(addtable);
       model.addAttribute("addtable",addTable);
       return "update";
    }

    @RequestMapping("update")
    public String Update(@Validated @ModelAttribute("addtable") AddTable addtable, BindingResult result, Model model){
        System.out.println(addtable+"--------------------*************");
        if(result.hasErrors()){
            //System.out.println(result.getAllErrors()+"----------------------------------------------------------------");
            model.addAttribute("addtable",addtable);
            return "update";
        }
       int i=addTableService.update(addtable);
        if(i>0){
            return "redirect:list";
        }
        return null;
    }

}
